package com.example.module_parking.ui

import androidx.lifecycle.MutableLiveData
import com.example.lib_common.base.BaseViewModel
import com.example.module_parking.model.AddParkingInfo
import com.example.module_parking.model.ParkingEntity
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class ParkingViewModel @Inject constructor(private val repository :ParkingRepository):BaseViewModel() {
    private val parkingList by lazy {
        MutableLiveData<List<ParkingEntity>>()
    }
    val parkingListLiveData get() = parkingList

    val addParking by lazy {
        MutableLiveData<AddParkingInfo>()
    }
    val addParkingLiveData get() = addParking

    private val editParking by lazy {
        MutableLiveData<AddParkingInfo>()
    }
    val editParkingLiveData get() = editParking


    private val searchParking by lazy {
        MutableLiveData<ParkingEntity>()
    }
    val searchParkingLiveData get() = searchParking

    fun getParkingList(spaceType:String){
        execute(
            action = repository.getParkingList(spaceType),
            onError = {
                parkingList.value = null
            },
            onFinish = {
                parkingList.value = it.data
            }
        )
    }
fun addParking(addParkingInfo: AddParkingInfo){
    execute(
        action = repository.addParking(addParkingInfo),
        onError = {
            addParking.value = null
        },
        onFinish = {
            addParking.value = it.data
        }
    )
}
    fun editParking(id:Int,addParkingInfo: AddParkingInfo){
        execute(
            action = repository.editParking(id,addParkingInfo),
            onError = {
                editParking.value = null
            },
            onFinish = {
                editParking.value = it.data
            }
        )
    }
    fun searchParking(id:Int){
        execute(
            action = repository.searchParking(id),
            onError = {
                searchParking.value = null
            },
            onFinish = {
                searchParking.value = it.data
            }
        )
    }
}